<template>
  <div class="app-container">
<!--TODO 查询列表 ele ===========================================================================================================================-->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <!--TODO 活动名称 查询--待完成 -->
      <el-form-item label="活动名称" prop="activityId">
        <el-select v-model="queryParams.activityId" placeholder="请选择活动名称" clearable>
          <el-option
            v-for="item in activityList"
            :key="item.id"
            :label="item.title"
            :value="item.id"
          />
        </el-select>
      </el-form-item>

      <!--      TODO 用户名称 查询--待完成 -->
      <el-form-item label="用户昵称" prop="userId">
        <el-select v-model="queryParams.userId" placeholder="请选择用户昵称" clearable>
          <el-option
            v-for="item in userList"
            :key="item.id"
            :label="item.nickname"
            :value="item.id"
          />
        </el-select>
      </el-form-item>

      <!--      TODO 活动状态查询--待完成 -->
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.activity_review"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>


      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['activitySignLog:activitySignLog:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['activitySignLog:activitySignLog:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['activitySignLog:activitySignLog:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['activitySignLog:activitySignLog:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

<!--TODO  主页 table数据展示 ele ===========================================================================================================================-->
    <el-table v-loading="loading" :data="activitySignLogList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="id" />

        <el-table-column label="活动" align="center" prop="activityId">
          <template slot-scope="scope">
            <div v-for="item in activityList" v-if="scope.row.activityId === item.id">{{item.title}}</div>
          </template>
        </el-table-column>

<!--      <el-table-column label="用户" align="center" prop="userId" />-->
      <el-table-column label="姓名" align="center" prop="userId">
        <template slot-scope="scope">
          <div v-for="item in userList" v-if="scope.row.userId === item.id">{{item.name}}</div>
        </template>
      </el-table-column>
      <el-table-column label="用户昵称" align="center" prop="userId">
        <template slot-scope="scope">
          <div v-for="item in userList" v-if="scope.row.userId === item.id">{{item.nickname}}</div>
        </template>
      </el-table-column>
<!--      <el-table-column label="姓名" align="center" prop="name" />-->

      <el-table-column label="手机号" align="center" prop="phone" />
      <el-table-column label="年龄" align="center" prop="age" />
      <el-table-column label="审核时间" align="center" prop="applyTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.applyTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.activity_review" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['activitySignLog:activitySignLog:edit']"
          >审核</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['activitySignLog:activitySignLog:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!--TODO 添加或修改活动报名对话框ele ↓↓↓↓↓↓↓↓↓↓↓↓↓====================================================================================================-->
    <!-- 添加或修改活动报名对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

<!--  TODO 活动 -->
        <el-form-item label="活动" prop="activityId">
          <el-select v-model="form.activityId" placeholder="请选择活动">
            <el-option
              v-for="item in activityList"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            />
          </el-select>
        </el-form-item>

<!--  TODO 用户 -->
        <el-form-item label="用户" prop="userId">
          <el-select v-model="form.userId" placeholder="请选择用户">
            <el-option
              v-for="item in userList"
              :key="item.id"
              :label="item.nickname"
              :value="item.id"
            />
          </el-select>
        </el-form-item>


<!--        <el-form-item label="姓名" prop="name">-->
<!--          <el-input v-model="form.name" placeholder="请输入姓名" />-->
<!--        </el-form-item>-->
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="form.age" placeholder="请输入年龄" />
        </el-form-item>
        <el-form-item label="审核时间" prop="applyTime">
          <el-date-picker
            v-model="form.applyTime"
            type="date"
            placeholder="选择日期"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态">
            <el-option
              v-for="dict in dict.type.activity_review"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>


<!--TODO script  ↓↓↓↓↓↓↓↓↓↓↓↓↓===============================================================================================================================-->
<script>
import { listActivitySignLog, getActivitySignLog, delActivitySignLog, addActivitySignLog, updateActivitySignLog, getActivityList, getUserList} from "@/api/activitySignLog/activitySignLog"

export default {
  name: "ActivitySignLog",
  dicts: ['activity_review'],
  data() {
    return {
      value1: '',
      userList: [],       // 存储后端传来的用户列表数据
      activityList: [],     // 存储后端传来的活动列表数据
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // TODO ==============================================================================
      // 活动报名表格数据--总数据
      activitySignLogList: [],
      // TODO ==============================================================================

      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        activityId: null,
        userId: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    }
  },
  created() {
    this.getList();//  页面加载时自动查询数据
    this.loadActivityList(); // 页面加载时获取活动列表
    this.loadUserList(); // 页面加载时获取用户列表
    // console.log("activityList:", this.activityList);

  },
  methods: {
    // 加载活动列表
    loadActivityList() {
      getActivityList().then(response => {// 每一条活动数据
        this.activityList = response.data; // 绑定数据到下拉框
      }).catch(error => {
        console.error('获取活动列表失败:', error);
      });
    },
    // 加载用户列表
    loadUserList() {
      getUserList().then(response => {// 每一条活动数据
        this.userList = response.data; // 绑定数据到下拉框
      }).catch(error => {
        console.error('获取用户列表失败:', error);
      });
    },
    /** 查询活动报名列表 */
    getList() {
      this.loading = true
      listActivitySignLog(this.queryParams).then(response => {
        this.activitySignLogList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        selectedActivity: null,
        id: null,
        activityId: null,
        userId: null,
        name: null,
        phone: null,
        age: null,
        applyTime: null,
        status: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        addressId: null,
        address: null,
        remark: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加活动报名"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getActivitySignLog(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改活动报名"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateActivitySignLog(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addActivitySignLog(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除活动报名编号为"' + ids + '"的数据项？').then(function() {
        return delActivitySignLog(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('activitySignLog/activitySignLog/export', {
        ...this.queryParams
      }, `activitySignLog_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
